package com.console.operations.planmgr;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;

import com.common.test.GetDataFromImportFile;
import com.common.test.OperationBase;
import com.common.test.UITestBase;
import com.common.util.DBUtil;
import com.common.util.GetTempleteFileUtil;
import com.console.pages.planmgr.FinancePlanBatchPage;

public class OhgFinancePlanBatchImport extends OperationBase {
	
	public OhgFinancePlanBatchImport(UITestBase tb){
		super(tb);
	}
	
	public boolean isinserted = false;
	
	public boolean isstatuscorrected = false;
	
	public boolean isfinanceamountequal = false;
	
	public void batchoperation() throws Exception{
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        Calendar cal = Calendar.getInstance();
        Date date = cal.getTime();
        String datestr = sdf.format(date);
        Random random = new Random();
        int x = random.nextInt(8999);
        x = x+1000;
        String tempstr = datestr + String.valueOf(x);
        String financeplantitle = GetDataFromImportFile.getOneLineDataFromCell(GetTempleteFileUtil.INVESTBATCH_OHG_TEMPLATE, "名称");
        System.out.println("financeplantitle is " + financeplantitle);
        if(DBUtil.isQueryData("select * from fiz_plan where vc_name = ?", new Object[]{financeplantitle})){
        	DBUtil.update("update fiz_plan set vc_name = ? where vc_name = ?", new Object[]{tempstr, financeplantitle});
        }
		switchToDefaultContent();
		FinancePlanBatchPage fpbp = new FinancePlanBatchPage(tb);
		waitForElementExist(fpbp.upLoadFile);
		//上传INVESTBATCH_OHG_TEMPLATE.xls文件
		fpbp.upLoadFile.chooseFile(GetTempleteFileUtil.INVESTBATCH_OHG_TEMPLATE);
		pause(700);
		//点击上传文件按钮
		fpbp.upLoadButton.click();
		waitForElementExist(fpbp.importButton);
		//点击导入按钮
		fpbp.importButton.click();
		waitForPageContainsStr("已完成理财计划导入");
		pause(1500);
		isinserted = DBUtil.isQueryData("select * from fiz_plan where vc_name = ?", new Object[]{financeplantitle});
		if(!isinserted){
			log.error("理财计划表中没能成功插入数据");
			return;
		}
		String dcstatus = DBUtil.queryOneCell("select dc_status from fiz_plan where vc_name = ?", new Object[]{financeplantitle});
		isstatuscorrected = dcstatus.equals("00");
		if(!isstatuscorrected){
			log.error("未审核前状态不为00（未审核）");
			return;
		}
		log.info("数据库里理财计划金额为 " + new BigDecimal(DBUtil.queryOneCell("select nb_amount from fiz_plan where vc_name = ?", new Object[]{financeplantitle})).setScale(2, BigDecimal.ROUND_DOWN).toString());
		log.info("excel里理财计划金额为 " + new BigDecimal(GetDataFromImportFile.getOneLineDataFromCell(GetTempleteFileUtil.INVESTBATCH_OHG_TEMPLATE, "金额")).setScale(2, BigDecimal.ROUND_DOWN).toString());
		isfinanceamountequal = new BigDecimal(DBUtil.queryOneCell("select nb_amount from fiz_plan where vc_name = ?", new Object[]{financeplantitle})).setScale(2, BigDecimal.ROUND_DOWN).toString().equals(new BigDecimal(GetDataFromImportFile.getOneLineDataFromCell(GetTempleteFileUtil.INVESTBATCH_OHG_TEMPLATE, "金额")).setScale(2, BigDecimal.ROUND_DOWN).toString());
		if(!isfinanceamountequal){
			log.error("导入的金额和excel中金额不一致");
			return;
		}
	}
}
